package tablet.module_user.fragment.teacherspace;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpDialogCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.UserRecordVideoBean;

/**
 * Created by Administrator on 2020/8/18 0018.
 */

@SuppressLint("ValidFragment")
public class UserRecordVideoFragment extends BaseAppFragment {

    @BindView(R2.id.rv_video)
    RecyclerView rvVideo;
    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    int teacherId = -1;

    int page = 0;
    HomeAdapter homeAdapter;
    List<UserRecordVideoBean.ObjBean.RowsBean> list = new ArrayList<>();

    public UserRecordVideoFragment(int teacherId) {
        this.teacherId = teacherId;
    }

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initRecordVideoData();
            }
        });

        initView();

        initRecordVideoData();

    }

    private void initRecordVideoData() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "RECORDING_AND_TEACHING_ASSISTANT")
                .addParam("token", SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken())
                .addParam("request_method", 0)
                .addParam("page", page)
                .addParam("limit", 10)
                .addParam("teacher_id", teacherId)
                .execute(new HttpDialogCallBack<UserRecordVideoBean>() {
                    @Override
                    public void onSuccess(UserRecordVideoBean result) {


                        if (CodeUtil.CODE_200 == result.getCode()) {
                            if (result.getObj() != null) {
                                if (page == 0 && list.size() > 0) {
                                    list.clear();
                                }
                                list.addAll(result.getObj().getRows());
                                homeAdapter.notifyDataSetChanged();
                            }

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {

                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }

                    @Override
                    public void onError(String e) {

                        stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                });

    }

    private void initView() {

        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                initRecordVideoData();

                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++page;
                initRecordVideoData();

                springView.onFinishFreshAndLoad();
            }
        });

        homeAdapter = new HomeAdapter(getActivity(), list);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        rvVideo.setLayoutManager(manager);
        rvVideo.setAdapter(homeAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_userecord;
    }


    private class HomeAdapter extends BaseAdapter<UserRecordVideoBean.ObjBean.RowsBean> {

        public HomeAdapter(Context context, List<UserRecordVideoBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_userecord;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<UserRecordVideoBean.ObjBean.RowsBean> mData) {

            final UserRecordVideoBean.ObjBean.RowsBean rowsBean = mData.get(position);

            ImageView ivBg = holder.getView(R.id.iv_record);
            if (StringUtil.isNotEmpty(rowsBean.getVideo_screenshot(), true)) {

                Glide.with(getActivity())
                        .load(rowsBean.getVideo_screenshot())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivBg);
            }
            if (StringUtil.isNotEmpty(rowsBean.getBig_title(), true)) {
                holder.setText(R.id.tv_record, rowsBean.getBig_title());
            }

            LinearLayout linItem = holder.getView(R.id.lin_item);
            linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ARouter.getInstance().build(ARouterPathUtils.Tablet_User_TeacherReordVideoListActivity)
                            .withInt("recordId", rowsBean.getId()).navigation();
                }
            });
        }
    }

}